﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WMS.Entity;
using WMS.BLL;

namespace WMS.App
{
    public partial class frmCategory : Form
    {
        private frmCategoryOperate categoryOp = new frmCategoryOperate();
        private Category _Category;

        #region Public Event
        public ResultEventHandler ResultCallBackEvent = null;
        #endregion

        public frmCategory()
        {
            InitializeComponent();

            InitData();
        }

        void InitData()
        {
            _Category = new Category();

            FillTree();
        }

        void FillTree()
        {
            TreeNode rootNode = this.treeView1.Nodes["RootNode"];
            rootNode.Nodes.Clear();
            IList<CategoryEntity> entityList = _Category.GetAllList();
            GetSubNode(rootNode, 0, entityList);

            rootNode.Expand();
            this.treeView1.Focus();
        }

        void SetComponetEnabled(bool value)
        {
            //this.btnAddSameLevelCategory.Enabled = value;
            //this.btnAddSubCategory.Enabled = value;
            this.btnModifySelectedCategory.Enabled = value;
            this.btnDeleteCategory.Enabled = value;
        }

        void GetSubNode(TreeNode parentNode, int parentNodeID, IList<CategoryEntity> nodeList)
        {
            #region
            IList<CategoryEntity> list = new List<CategoryEntity>();

            foreach (CategoryEntity entity in nodeList)
            {
                if (entity.IsDeleted == 0 && entity.ParentId == parentNodeID)
                {
                    TreeNode node = new TreeNode();
                    node.Text = entity.Name;
                    node.Name = entity.CategoryId.ToString();

                    parentNode.Nodes.Add(node);

                    GetSubNode(node, entity.CategoryId, nodeList);
                }
            }
            #endregion
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            #region
            if (e.Node.Name == "RootNode")
            {
                this.btnModifySelectedCategory.Enabled = false;
                this.btnDeleteCategory.Enabled = false;
            }
            else
            {
                this.btnModifySelectedCategory.Enabled = true;
                if (e.Node.Nodes.Count > 0)
                {
                    this.btnDeleteCategory.Enabled = false;
                }
                else
                {
                    this.btnDeleteCategory.Enabled = true;
                }
            }
            this.lblTreeNodeFullPath.Text = e.Node.FullPath;
            #endregion
        }

        private void Button_Click(object sender, EventArgs e)
        {
            #region
            Button btn = ((Button)sender);
            CategoryAction action = CategoryAction.Add;
            TreeNode node = this.treeView1.SelectedNode;
            switch (btn.Name)
            {
                case "btnAddSameLevelCategory":
                    action = CategoryAction.Add;
                    break;
                case "btnAddSubCategory":
                    action = CategoryAction.AddSubCategory;
                    break;
                case "btnModifySelectedCategory":
                    action = CategoryAction.Modify;
                    break;
                case "btnDeleteCategory":
                    action = CategoryAction.Delete;
                    if (node.Nodes.Count == 0 && MessageBoxCommon.ConfirmDelete(node.Text))
                    {
                        Category cate = new Category();
                        cate.Delete(int.Parse(node.Name));

                        this.treeView1.Nodes.Remove(node);
                    }
                    return;
                case "btnReload":
                    SetComponetEnabled(false);
                    FillTree();
                    this.lblTreeNodeFullPath.Text = this.treeView1.Nodes["RootNode"].Text;
                    return;
                default: return;
            }

            if (node.Name == "RootNode")
            {
                categoryOp.SetFormText(btn.Text, 0, 0, node.Text, action, node);
            }
            else
            {
                int parentID = node.Parent.Name == "RootNode" ? 0 : int.Parse(node.Parent.Name);
                categoryOp.SetFormText(btn.Text, int.Parse(node.Name), parentID, node.Text, action, node);
            }

            categoryOp.ShowDialog();
            categoryOp.Focus();
            //FillTree();

            #endregion
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.btnOK_Click(sender,e);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Hide();
            if (ResultCallBackEvent != null)
            {
                TreeNode node = this.treeView1.SelectedNode;
                ResultEventArgs args = new ResultEventArgs();
                Dictionary<string, string> dic = new Dictionary<string, string>();
                if (node.Name != "RootNode")
                {
                    args.ResultCode = int.Parse(node.Name);
                    args.Result = node.FullPath.Substring(this.treeView1.Nodes[0].Text.Length);
                }
                
                ResultCallBackEvent(this,args);
            }
        }
    }
}
